// Package handler
// @Author zhongxc
// @Date 2024/1/16 16:25:00
// @Desc
package handler

import (
	"errors"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"strconv"
	common "zhongxc.cc/admin-common"
	"zhongxc.cc/admin-common/errs"
	"zhongxc.cc/admin-user-api/internal/logic"
	"zhongxc.cc/admin-user-api/internal/svc"
)

type MenuHandler struct {
	svcCtx *svc.ServiceContext
}

func NewMenuHandler(svcCtx *svc.ServiceContext) *MenuHandler {
	return &MenuHandler{
		svcCtx: svcCtx,
	}
}

func (m *MenuHandler) GetMenuList(w http.ResponseWriter, r *http.Request) {
	menuLogic := logic.NewMenuLogic(r.Context(), m.svcCtx)

	page := r.URL.Query().Get("page")
	rows := r.URL.Query().Get("rows")
	keyword := r.URL.Query().Get("keyword")

	pageNum, err := strconv.ParseInt(page, 10, 64)
	if err != nil {
		pageNum = 1
	}
	rowsNum, err := strconv.ParseInt(rows, 10, 64)
	if err != nil {
		rowsNum = 10
	}

	list, err := menuLogic.GetMenuList(pageNum, rowsNum, keyword)
	code, msg := errs.ParseGrpcError(err)
	httpx.OkJsonCtx(r.Context(), w, common.NewResult().Ok(list, code, errors.New(msg)))
}
